package net.alteiar;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;

import net.alteiar.dungeon.generator.MapBuilder;
import net.alteiar.images.BasicImage;
import net.alteiar.images.ImageFiltered;
import net.alteiar.images.filter.SimpleFilter;
import net.alteiar.images.filter.elements.CompleteVisionFilter;
import net.alteiar.images.filter.elements.ElementFilter;
import net.alteiar.images.metadata.EntityInfo;
import net.alteiar.images.metadata.ImageInfo;
import net.alteiar.images.metadata.ImageInfoBuilder;
import net.alteiar.images.metadata.VisionInfo;
import net.alteiar.ui.MovablePanel;
import net.alteiar.ui.VisionImagePanel;
import net.alteiar.ui.ZoomablePanel;
import net.alteiar.ui.listener.MoveListener;
import net.alteiar.ui.listener.ZoomListener;

public class Main {

	/**
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		testClusteringEtc();

		// testBuildRandomMap();
	}

	public static void testBuildRandomMap() {
		MapBuilder builder = new MapBuilder();
		Array2D map2D = builder.buildMap();

		final BasicImage im = new BasicImage(map2D.buildImage());

		// VisionImageFilter vision = new VisionImageFilter(inf);

		ImageFiltered finalImage = new ImageFiltered(im);
		// finalImage.addImage(filter);

		// VisionImagePanel<ImageFiltered> panelImg = new VisionImagePanel<>(
		// finalImage, vision);

		ZoomablePanel<ImageFiltered> panelImg = new ZoomablePanel<>(finalImage);

		MovablePanel move = new MovablePanel(panelImg);
		move.setPreferredSize(new Dimension(800, 600));

		MoveListener moveListener = new MoveListener(move);
		panelImg.addMouseWheelListener(new ZoomListener(panelImg));
		panelImg.addMouseMotionListener(moveListener);
		panelImg.addMouseListener(moveListener);

		final JFrame frame = new JFrame("test");
		frame.add(move);
		frame.pack();
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	public static void testClusteringEtc() throws IOException {
		final JFrame frame = new JFrame("test");

		// String file = "VisionTest.jpg";
		String file = "Camp_Gobelin.jpg";
		// String file = "Ruine_du_chateau_de_Trevilis.JPG";
		// String file = "Taverne_de_l_orphelin_rond_resize.jpg";
		// String file = "colline_simple_no_grid.jpg";
		BufferedImage img = ImageIO.read(new File("test-imgs/" + file));

		final BasicImage im = new BasicImage(img);
		final BasicImage auroch = new BasicImage(ImageIO.read(new File(
				"test-imgs/" + "Auroch.png")));

		// ImageInfo inf = RGBExtractor.extractRed(im);
		// ImageInfo inf = RGBExtractor.extractRed(im);
		// ImageInfo colors = RGBExtractor.extractBlue(im);

		// ImageInfo colors = ImageInfoUtils.buildAverage(im, 5);
		// ImageInfo colors = new ImageInfo(im, 2);
		// ImageInfo inf = ClusteringUtils.createKMeansGraphics(colors,
		// new ImageRGBBuilder(), 5);

		// inf.getMaps().replaceValues(1, Integer.MAX_VALUE);
		// inf.getMaps().replaceValues(2, Integer.MAX_VALUE);
		// inf.getMaps().replaceValues(3, Integer.MAX_VALUE);
		// inf.getMaps().replaceValues(4, Integer.MAX_VALUE);
		ImageInfo inf = new ImageInfo(im, 1);
		inf.fill(1);

		ImageInfo info = new ImageInfo(im, 15);
		info.fill(1);

		int max = 2;
		int minx = 10 * 30;
		int maxx = minx + 5 * 30;

		int miny = 10 * 30;
		int maxy = miny + 5 * 30;

		for (int x = minx; x < maxx; ++x) {
			for (int y = miny; y < maxy; ++y) {
				info.setValueAt(x, y, max);
			}
		}

		minx = 20 * 30;
		maxx = minx + 5 * 30;

		miny = 20 * 30;
		maxy = miny + 5 * 30;

		for (int x = minx; x < maxx; ++x) {
			for (int y = miny; y < maxy; ++y) {
				info.setValueAt(x, y, 3);
			}
		}

		CompleteVisionFilter visionFilter = new CompleteVisionFilter(info);

		EntityInfo entity = new EntityInfo(new Point(5 * 30, 5 * 30), 30, 30);
		VisionInfo visionInfo = new VisionInfo(entity, inf);
		visionFilter.addVision(visionInfo);

		SimpleFilter grid = new SimpleFilter(ImageInfoBuilder.buildGrid(im, 30));

		ImageFiltered finalImage = new ImageFiltered(im);

		// Fixed
		finalImage.addImage(new ElementFilter(entity, auroch));
		finalImage.addImage(grid);
		finalImage.addImage(visionFilter);
		// finalImage.addImage(new BlockingFilter(info));

		// VisionImagePanel<ImageFiltered> panelImg = new VisionImagePanel<>(
		// finalImage, vision);
		ZoomablePanel<ImageFiltered> panelImg = new VisionImagePanel<ImageFiltered>(
				finalImage, entity);

		MovablePanel move = new MovablePanel(panelImg);
		move.setPreferredSize(new Dimension(600, 600));

		MoveListener moveListener = new MoveListener(move);
		panelImg.addMouseWheelListener(new ZoomListener(panelImg));
		panelImg.addMouseMotionListener(moveListener);
		panelImg.addMouseListener(moveListener);

		frame.add(move);
		frame.pack();
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		/*
		inf.setValue(5, 0, 500);

		// JDialog dlg = new JDialog(frame);
		// ImageInfoEditorVision editor = new ImageInfoEditorVision(im, inf);
		// MovablePanel moveDlg = new MovablePanel(editor);
		// moveDlg.setPreferredSize(new Dimension(600, 600));

		// MoveListener moveListenerDlg = new MoveListener(moveDlg);
		// editor.addMouseWheelListener(new ZoomListener(editor));
		// editor.addMouseMotionListener(moveListenerDlg);
		// editor.addMouseListener(moveListenerDlg);

		// dlg.add(editor);
		// dlg.pack();
		// dlg.setVisible(true);

		// ImageInfoEditorPanel editor = new ImageInfoEditorPanel(im, inf);

		PanelAlgorithms clusteringPanel = new PanelAlgorithms(im);

		frame.add(clusteringPanel);
		frame.pack();
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		*/
	}
}
